package com.eunion.web.cms.controller;

import com.eunion.web.common.base.BaseService;
import com.eunion.web.common.base.BaseTableCommonController;
import com.eunion.web.common.vo.Web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eunion.core.LogUtil;
import com.eunion.core.common.DeleteVo;
import com.eunion.core.common.JsonVo;
import com.eunion.web.cms.entity.ExpertInfo;
import com.eunion.web.cms.service.ExpertInfoService;

@Controller
@RequestMapping("/api/expertinfo")
@PreAuthorize("hasAuthority('volunteer:expertinfo')")
public class ExpertInfoResource extends BaseTableCommonController<ExpertInfo, Long> {

    @Autowired
    private ExpertInfoService service;

    @Override
    protected BaseService<ExpertInfo, Long> getBaseService() {
        // TODO Auto-generated method stub
        return service;
    }

    /**
     * 进入专家信息界面
     * 
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return getUrlBase();
    }

    @Override
    protected String getUrlBase() {
        return "admin/expert/expertinfo";
    }

    @PostMapping(value = Web.URL_SEP_SAVE)
    public String save(ExpertInfo obj, BindingResult result, Model model, HttpServletRequest request) {
        getBaseService().save(obj);
        setModuleStatusOK(model, "新增成功");
        setModuleAdd(model);
        return getUrlBase().concat("-add");
    }

    @PostMapping(value = Web.URL_SEP_UPDATE)
    public String update(ExpertInfo obj, BindingResult result, Model model, HttpServletRequest request) {
        getBaseService().save(obj);
        setModuleStatusOK(model, "更新成功");
        setModuleAdd(model);
        return getUrlBase().concat("-edit");
    }

    /**
     * 根据传入的数据ids,来删除对象.
     */
    @PostMapping(value = Web.URL_SEP_REMOVE)
    @ResponseBody
    protected JsonVo remove(@RequestBody DeleteVo<Long> dvo) {
        try {
            if (dvo != null && dvo.getIds() != null && dvo.getIds().size() > 0) {
                return super.remove(dvo);
            }
            return new JsonVo(true);
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("remove error ", e);
            return new JsonVo(false, "删除失败");
        }
    }
}
